package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.*;
import com.example.demo.server.controller.ArticleController;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;

public class DetailView implements View{

    private ArticleController articleController = new ArticleController();

    @Override
    public CommonRequest jump(CommonRequest request) {
        return showPage(request);

    }

    /**
     *  展示页面
     */
    public CommonRequest showPage(CommonRequest request) {
        UserInfo userInfo = SessionUtils.getUser();
        ArticleInfo articleInfo = (ArticleInfo) request.getBody();
        CommonResponse response = articleController.detail(articleInfo);
        if(userInfo == null) {
            return showPageNoLogin((MyHashMap<String, Object>) response.getData());
        }else {
            return showPageLogin(userInfo, (MyHashMap<String, Object>) response.getData());
        }
    }



    public CommonRequest showPageLogin(UserInfo userInfo, MyHashMap<String, Object> data) {
        ArticleInfo articleInfo = (ArticleInfo) data.get("art");
        UserInfo user = (UserInfo) data.get("user");
        int count = (int) data.get("count");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]    主页（list）  我的博客（mylist）  写博客（write）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("|           +—————————————+                                                      |");
        System.out.printf("|           |   ∩     ∩   |     author: %-10s                               |\n", user.getName());
        System.out.printf("|           |      V      |   articles: %-3d                                      |\n", count);
        System.out.println("|           |_____________|                                                      |");
        System.out.println("|________________________________________________________________________________|");
        ArticleUtils.printArticle(articleInfo);
        System.out.println("|________________________________________________________________________________|");
        String command = APPUtils.input();
        MyList<String> myList = APPUtils.handleCommand(command);
        if(myList.size() == 1) {
            String view = myList.get(0);
            if(ViewKey.DETAIL.equals(view)) {
                return CommonRequest.createRequest(ViewKey.DETAIL, data.get("art"));
            }
            CommonRequest commonRequest = APPUtils.checkJump(view,
                    ViewKey.LOGOUT, ViewKey.WRITE, ViewKey.LIST, ViewKey.MY_LIST);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again!");
        return CommonRequest.createRequest(ViewKey.DETAIL, data.get("art"));
    }
    public CommonRequest showPageNoLogin(MyHashMap<String, Object> data) {
        ArticleInfo articleInfo = (ArticleInfo) data.get("art");
        UserInfo user = (UserInfo) data.get("user");
        int count = (int) data.get("count");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("| [未登录]                                          主页（list）   登录（login）     |");
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("|           +—————————————+                                                      |");
        System.out.printf("|           |   U     U   |     author: %-10s                               |\n", user.getName());
        System.out.printf("|           |      Λ      |   articles: %-3d                                      |\n", count);
        System.out.println("|           |_____________|                                                      |");
        System.out.println("|________________________________________________________________________________|");
        ArticleUtils.printArticle(articleInfo);
        System.out.println("|________________________________________________________________________________|");
        String command = APPUtils.input();
        MyList<String> myList = APPUtils.handleCommand(command);
        if(myList.size() == 1) {
            String view = myList.get(0);
            if(ViewKey.DETAIL.equals(view)) {
                return CommonRequest.createRequest(ViewKey.DETAIL, data.get("art"));
            }
            CommonRequest commonRequest = APPUtils.checkJump(view, ViewKey.LIST, ViewKey.LOGIN);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again!");
        return CommonRequest.createRequest(ViewKey.DETAIL, data.get("art"));
    }
}
